package com.atguigu.gulimall.order.web;


import com.alipay.api.AlipayApiException;
import com.atguigu.gulimall.order.config.AlipayTemplate;
import com.atguigu.gulimall.order.config.PayVo;
import com.atguigu.gulimall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PayWebController {



    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private OrderService orderService;


    @ResponseBody
    @GetMapping(value = "payOrder",produces = MediaType.TEXT_HTML_VALUE)//  去支付订单.    去掏钱!
    public String payOrder(@RequestParam  String orderSn) throws AlipayApiException {  //  传入一个参数:订单编号
        PayVo payVo =  orderService.getOrderPay(orderSn);  //   返回一个订单主要信息
        String pay = alipayTemplate.pay(payVo);  //  然后去支付.是调用的支付宝官方提供的方法  ,返回的pay是一个页面
         //  这个pay是一个页面   把这个页面直接交给浏览器
        System.out.println("pay = " + pay);
        return pay;
    }


}
